#include "HttpDeviceInfoManager.h"
#include <unistd.h>
#include "log.h"


namespace Media{

mutex HttpDeviceInfoManager::instanceLock;
HttpDeviceInfoManager *HttpDeviceInfoManager::instanceDeviceInfoManager = nullptr;   

HttpDeviceInfoManager *HttpDeviceInfoManager::instance(){
    if (instanceDeviceInfoManager == nullptr) {
        std::lock_guard<std::mutex> lock(instanceLock);
        if (instanceDeviceInfoManager == nullptr) {
            instanceDeviceInfoManager = new HttpDeviceInfoManager();
        }
    }
    return instanceDeviceInfoManager;
}


bool HttpDeviceInfoManager::Subscribe(function<bool(Json::Value table)> cb){
    auto lam = [this, cb](){
        while(true){
            DeviceInfo devInfo;
            if(devInfo.getDevInfo()){
               if(!devInfo.httpInfoCompare(m_devInfo)){
                    Json::Value table;
                    devInfo.gethttpJson(table);
                    cb(table);
                    m_devInfo = devInfo;
                }
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 3));
        }
    };
    thread deviceInfo(lam);
    deviceInfo.detach();
	return true;
}


bool HttpDeviceInfoManager::GetDeviceList(Json::Value &table){
    DeviceInfo devInfo;
    if(devInfo.getDevInfo()){
        //if(!devInfo.httpInfoCompare(m_devInfo)){
            devInfo.gethttpJson(table);
            m_devInfo = devInfo;
        //}
    }else{
        LOG_PrintWarn("faild to get device info\n");
        return false;
    }
    return true;
}



HttpDeviceInfoManager::HttpDeviceInfoManager(){
}



}